<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Events;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

use Events\model\Event;
use Events\model\EventTable;
use Events\model\Register;
use Events\model\RegisterTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway; 
use Zend\Db\TableGateway\Feature\MetadataFeature;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        
        $eventManager->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($event) {
        	$controller      = $event->getTarget();
        	$controllerName  = get_class($controller);
        	$moduleNamespace = substr($controllerName, 0, strpos($controllerName, '\\'));
        	$configs         = $event->getApplication()->getServiceManager()->get('config');
        	if (isset($configs['moduleLayouts'][$moduleNamespace])) {
        		$controller->layout($configs['moduleLayouts'][$moduleNamespace]);
        	}
        }, 100);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
    
    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Event\Model\EventTable' =>  function($sm) {
                    $tableGateway = $sm->get('EventTableGateway');
                    $table = new EventTable($tableGateway);
                    return $table;
                },
                'EventTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Event());
                    return new TableGateway('event', $dbAdapter, new MetadataFeature(), $resultSetPrototype);
                },
                'Event\Model\RegisterTable' =>  function($sm) {
                    $tableGateway = $sm->get('RegisterTableGateway');
                    $table = new RegisterTable($tableGateway);
                    return $table;
                },
                'RegisterTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Register());
                    return new TableGateway('register', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }
    
}
